package com.pheruagor.handmanager.view {
    import com.pheruagor.handmanager.model.business.event.ValidationEvent;
    import com.pheruagor.handmanager.view.component.ValidationPopUp;

    import flash.events.MouseEvent;

    import mx.events.FlexEvent;

    import org.puremvc.as3.patterns.mediator.Mediator;

    public class ValidationPopUpMediator extends Mediator {
        public static const NAME:String = "validationPopUpMediator";
        //-----------------------------------------------------
        // Properties
        //-----------------------------------------------------
        [Bindable]
        private var m_validationTitle:String;
        [Bindable]
        private var m_validationLabel:String;

        //-----------------------------------------------------
        // Initialization
        //-----------------------------------------------------
        public function ValidationPopUpMediator(view:ValidationPopUp) {
            super(NAME, view);
        }

        //-----------------------------------------------------
        // IMediator Interface
        //-----------------------------------------------------
        override public function onRegister():void {
            validationPopUp.addEventListener(FlexEvent.CREATION_COMPLETE, onValidationPopUpCreationCompleted);
        }

        //-----------------------------------------------------
        // Access
        //-----------------------------------------------------
        public function get validationPopUp():ValidationPopUp {
            return ValidationPopUp(viewComponent);
        }

        public function set validationTitle(value:String):void {
            if (m_validationTitle != value) {
                m_validationTitle = value;
                if (validationPopUp != null) {
                    validationPopUp.title = m_validationTitle;
                }
            }
        }

        public function set validationLabel(value:String):void {
            if (m_validationLabel != value) {
                m_validationLabel = value;
                if (validationPopUp != null && validationPopUp.validationLabel != null) {
                    validationPopUp.validationLabel.text = value;
                }
            }
        }

        //-----------------------------------------------------
        // Event handling
        //-----------------------------------------------------
        private function onValidationPopUpCreationCompleted(event:FlexEvent):void {
            validationPopUp.okButton.addEventListener(MouseEvent.CLICK, onOkButtonClicked);
            validationPopUp.cancelButton.addEventListener(MouseEvent.CLICK, onCancelButtonClicked);
            if (validationPopUp != null) {
                validationPopUp.title = m_validationTitle;
            }
            if (m_validationLabel != null) {
                validationPopUp.validationLabel.text = m_validationLabel;
            }
        }

        private function onOkButtonClicked(event:MouseEvent):void {
            validationPopUp.dispatchEvent(new ValidationEvent(ValidationEvent.OK, true));
        }

        private function onCancelButtonClicked(event:MouseEvent):void {
            validationPopUp.dispatchEvent(new ValidationEvent(ValidationEvent.CANCEL, true));
        }
        //-----------------------------------------------------
        // Operation
        //-----------------------------------------------------

        //-----------------------------------------------------
        // Implementation
        //-----------------------------------------------------


    }
}